import cv2
"""
OpenCV3中常用的背景分割器有三种：Mixture of Gaussians(MOG2)、K-Nearest(KNN)、Geometric Multigid(GMG)
"""
def filter_img(frame):
	# kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2, 2))
	# # Fill any small holes
	# closing = cv2.morphologyEx(frame, cv2.MORPH_CLOSE, kernel)
	# # Remove noise
	# opening = cv2.morphologyEx(closing, cv2.MORPH_OPEN, kernel)
	# # Dilate to merge adjacent blobs
	# dilated = cv2.dilate(opening, kernel, iterations=2)

	_, thresh = cv2.threshold(frame, 244, 255, cv2.THRESH_BINARY)
	thresh = cv2.erode(thresh, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations = 2)
	dilated = cv2.dilate(thresh, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (8, 3)), iterations = 2)

	return dilated

# Object detection from Stable camera
object_detector = cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=40, detectShadows=True)
# object_detector = cv2.createBackgroundSubtractorKNN(detectShadows = True)
# object_detector = cv2.bgsegm.createBackgroundSubtractorGMG()

cap = cv2.VideoCapture("videos/traffic.mp4")

while True:
	ret, frame = cap.read()

	# 1. Object Detection
	mask = object_detector.apply(frame)
	mask = filter_img(mask)

	contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
	# for contour in contours:
	# 	(x, y, w, h) = cv2.boundingRect(contour)
	# 	if cv2.contourArea(contour) < 500:
	# 		continue
	# 	cv2.rectangle(frame, pt1=(x, y), pt2=(x+w, y+h), color=(0, 255, 0), thickness=2)
	cv2.drawContours(frame, contours, -1, (0,255, 0), 2)
		
	# show the output frame
	cv2.imshow("Frame", frame)
	cv2.imshow("Mask", mask)
	
	if cv2.waitKey(30) == ord("q"):
		break

cap.release()
# close all windows
cv2.destroyAllWindows()